#include "UViewPortWindow.h"
#include "UGUI.h"
#include "../UVector.h"
#include "../ULeafWindow.h"
#include "../UNonSpaceFillingContainerWindow.h"

using namespace std;

GLdouble model_view[16];
GLdouble projection[16];
GLint viewport[4];

TViewPortWindow::TViewPortWindow(TGUI* GUI)
: TCustomWindow(false)
{
	FGUI = GUI;
    Zoomfactor = 200;
	RMDown = false;
	MBDown = false;
	PosX = -64;
	PosY = -64;
	Rotation = 0;
	Tilt	 = -45;
}

void TViewPortWindow::PositionCamera()
{
	glViewport(Parent->Left,ScreenHeight-Parent->Top-Parent->Height(),Parent->Width(), Parent->Height());
	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
	glLoadIdentity();									// Reset The Projection Matrix
	gluPerspective(45.0f,(GLfloat)Parent->Width()/(GLfloat)Parent->Height(),0.1f,1000.0f);
	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
	glLoadIdentity();

	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
	glDepthFunc(GL_LESS);								// The Type Of Depth Testing To Do
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
	//////////////Set up opengl for drawing 3d/////////////////

	glLoadIdentity();

	glCullFace(GL_BACK);
	glEnable(GL_CULL_FACE);

	glTranslatef(0, 0, -Zoomfactor); // zoom out before Tilting
	glRotatef(Tilt, 1, 0, 0);

	glRotatef(Rotation, 0, 0, 1);
	glTranslatef(PosX, PosY, 0); // positioning after rotation
}

void TViewPortWindow::Draw( )
{
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);

	//set window to 3d mode
		PositionCamera();
	//window set to 3d mode

	//needed for transformation calculating functions (2d->3d & 3d<-2d)
		glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
		glGetDoublev(GL_PROJECTION_MATRIX, projection);
		glGetIntegerv(GL_VIEWPORT, viewport); //screen coorrdinates
	//needed for transformation calculating functions (2d->3d & 3d<-2

    FGUI->Draw();

	glDisable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glDisable(GL_LIGHTING);
	glDisable(GL_LIGHT0);

	//Back To 2d
		glViewport(0,0,ScreenWidth, ScreenHeight);
		//////////////Set up opengl for drawing 2d/////////////////
		glDisable(GL_DEPTH_TEST);
		glShadeModel(GL_SMOOTH);
		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		glOrtho(0, ScreenWidth, ScreenHeight, 0, -1, 1);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	//now in 2d render mode
}

void TViewPortWindow::MouseMove(int X, int Y, int mstate)
{
	if (RMDown)
	{
		PosX+= (X-MDownX)/3 * cos(Rotation*3.1415*180) -  (MDownY-Y)/3* sin(Rotation*3.1415*180);
		PosY+= (X-MDownX)/3 * sin(Rotation*3.1415*180) +  (MDownY-Y)/3* cos(Rotation*3.1415*180);
	}
	else
	if (MBDown)
	{
		Rotation += MDownX-X;
		Tilt= min( max (-80.0f, Tilt+MDownY-Y), 0.0f);
	}
	else 
	{
	//TODO act to current game's mouse cursor
	}

	MDownX = X;
	MDownY = Y;
}

void TViewPortWindow::MouseDown(int X, int Y, uint8 button)
{
	MDownX = X;
	MDownY = Y;

	if (button == SDL_BUTTON_RIGHT)
	{
		RMDown = true;
	}
	if ( button == SDL_BUTTON_MIDDLE)
    {
        MBDown = true;
    }

	//TODO act to current game's mouse cursor

}

void TViewPortWindow::MouseUp(int X, int Y, uint8 button)
{
	RMDown = false;
	MBDown = false;
//TODO act to current game's mouse cursor
}

void TViewPortWindow::MouseLeave()
{
	RMDown = false;
	MBDown = false;
//TODO act to current game's mouse cursor
}

